package cn.itsource.service.impl;

import cn.hutool.crypto.SecureUtil;
import cn.itsource.annotation.Logs;
import cn.itsource.domain.Logininfo;
import cn.itsource.domain.Menu;
import cn.itsource.jwt.JwtUtil;
import cn.itsource.jwt.Payload;
import cn.itsource.mapper.LogininfoMapper;
import cn.itsource.query.LogininfoQuery;
import cn.itsource.service.ILogininfoService;
import cn.itsource.util.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.junit.platform.commons.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class LogininfoServiceImpl implements ILogininfoService {
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Logs
    @Transactional
    @Override
    public void add(Logininfo logininfo) {
        logininfoMapper.add(logininfo);
    }
    @Logs
    @Transactional
    @Override
    public void update(Logininfo logininfo) {
        logininfoMapper.update(logininfo);
    }
    @Logs
    @Transactional
    @Override
    public void del(Long id) {
        logininfoMapper.del(id);
    }
    @Logs
    @Transactional
    @Override
    public void delList(List<Long> ids) {
        logininfoMapper.delList(ids);
    }

    @Override
    public Logininfo get(Logininfo logininfo) {
        return logininfoMapper.get(logininfo);
    }

    @Override
    public List<Logininfo> getAll() {
        return logininfoMapper.getAll();
    }
    @Override

    public PageInfo<Logininfo> pageQuery(LogininfoQuery query) {
        PageHelper.startPage(query.getCurrentPage(),query.getPageSize());
        List<Logininfo> logininfos = logininfoMapper.pageQuery(query);
        return new PageInfo<>(logininfos);
    }

    @Override
    public R login(Map<String, String> map) {
        String account = map.get("username");
        String password = map.get("password");
        String type = map.get("type");
        //校验空值
        if (StringUtils.isBlank(account) || StringUtils.isBlank(password)){
            return R.fail("账号或密码不能为空");
        }
            //效验账号数据库是否存在
        Logininfo logininfo = logininfoMapper.findByAccount(map);
        if (logininfo == null){
            return R.fail("账号不存在");
        }

        //如果存在则效验密码是否正确
        String inputmd5pwd = SecureUtil.md5(password + logininfo.getSalt());
        if (!inputmd5pwd.equals(logininfo.getPassword())){
            return R.fail("密码错误");
        }
        if (!logininfo.getDisable()){
            return R.fail("账号被禁用");
        }
        //密码和账号都正确
        Payload payload = new Payload();
        //将密码设置为null 保证信息的安全
        logininfo.setPassword(null);
        payload.setLogininfo(logininfo);
            //查权限
            List<String> permissions = logininfoMapper.findPermissionsByLogininfo(logininfo);
            //查菜单
            List<Menu> menus = logininfoMapper.findMenusByLogininfo(logininfo);
            payload.setPermissions(permissions);
            payload.setMenus(menus);
            String JwtToken = JwtUtil.generateToken(payload);


        return R.ok("登录成功",JwtToken);
    }


}

